﻿using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL
{
    public class PickListItem
    {
        #region private variables

        private System.Xml.Linq.XElement m_XElement;

        private string m_Label;
        private string m_Value;

        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickList m_PickList;

        #endregion

        internal PickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickList i_PickList, string i_Value) :
            this(i_PickList, i_Value, i_Value)
        {
        }

        internal PickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickList i_PickList, string i_Label, string i_Value)
        {
            this.PickListItemInitialize
                (
                i_PickList,
                i_Label,
                i_Value
                );

            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SimpleFeedBindingsXNamespace + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ITEM_ELEMENT_NAME);

            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LABEL_ATTRIBUTE, m_Label);
            m_XElement.SetValue(m_Value);
        }

        internal PickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickList i_PickList, System.Xml.Linq.XElement i_PickListItemXElement)
        {
            bool InvalidElement =
                (i_PickListItemXElement.Name.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ITEM_ELEMENT_NAME) ||
                (i_PickListItemXElement.Name.NamespaceName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_PickListItemXElement!");

            if (i_PickListItemXElement.Document != i_PickList.XElement.Document)
                i_PickListItemXElement = new System.Xml.Linq.XElement(i_PickListItemXElement);

            m_XElement = i_PickListItemXElement;

            string Label = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LABEL_ATTRIBUTE).Value;
            string Value = m_XElement.Value;

            this.PickListItemInitialize
                (
                i_PickList,
                Label,
                Value
                );
        }

        internal void PickListItemInitialize(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickList i_PickList, string i_Label, string i_Value)
        {
            if (System.String.IsNullOrEmpty(i_Value))
                throw new System.ArgumentException("Invalid value for i_Value!");

            m_PickList = i_PickList;
            m_Label = i_Value;
            m_Value = i_Value;

            if (!System.String.IsNullOrEmpty(i_Label))
                m_Label = i_Label;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.PickList PickList
        {
            get
            {
                return m_PickList;
            }
        }

        public string Label
        {
            get
            {
                return m_Label;
            }
        }

        public string Value
        {
            get
            {
                return m_Value;
            }
        }

        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        public override string ToString()
        {
            return m_Label;
        }
    }
}
